package bb.lanxing.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import androidx.core.content.ContextCompat;

import bb.lanxing.R;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.map.SlopeUtils;

public class SpliceBitmapUtil {
    public static Drawable spliceIcons(Context context, int sizeInDip, Drawable... drawables) {
        if (drawables == null || drawables.length == 0) {
            return null;
        }
        int dp2px = DensityUtil.dp2px(sizeInDip);
        int dp2px2 = DensityUtil.dp2px(context, 5.0f);
        int i = 0;
        for (Drawable drawable : drawables) {
            i += dp2px + dp2px2;
        }
        Bitmap createBitmap = Bitmap.createBitmap(i - dp2px2, dp2px, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        int i2 = 0;
        for (Drawable drawable2 : drawables) {
            int i3 = (dp2px - dp2px) / 2;
            drawable2.setBounds(i2, i3, i2 + dp2px, i3 + dp2px);
            drawable2.draw(canvas);
            i2 += dp2px + dp2px2;
        }
        return new BitmapDrawable(context.getResources(), createBitmap);
    }

    public static Drawable spliceIcons(Context context, List<Integer> dwIds, int sizeInDip) {
        if (dwIds == null || dwIds.isEmpty()) {
            return null;
        }
        if (dwIds.size() == 1) {
            int dp2px = DensityUtil.dp2px(DensityUtil.dp2px(sizeInDip));
            return BitmapHelper.zoomDrawable(ContextCompat.getDrawable(context, dwIds.get(0)), dp2px, dp2px);
        }
        Drawable[] drawableArr = new Drawable[dwIds.size()];
        for (int i = 0; i < dwIds.size(); i++) {
            drawableArr[i] = ContextCompat.getDrawable(context, dwIds.get(i));
        }
        return spliceIcons(context, sizeInDip, drawableArr);
    }

    public static Drawable generateWorkoutFeatures(Context context, int cadenceSrc, int heartSrc,
                                                   int locSrc, boolean isSegment, String slopeGradeMax, boolean isThreedWorkout) {
        return generateWorkoutFeatures(context, cadenceSrc, heartSrc, locSrc, isSegment, isThreedWorkout, slopeGradeMax, 16);
    }

    public static Drawable generateWorkoutFeatures(Context context, int cadenceSrc, int heartSrc,
                                                   int locSrc, boolean isSegment, boolean is3dWorkout,
                                                   String slopeGradeMax, int sizeInDip) {
        ArrayList<Integer> arrayList = new ArrayList<>();

        if (cadenceSrc == Workout.CADENCE_SOURCE_BICI) {
            arrayList.add(R.drawable.ic_workout_list_bici);
        } else if (cadenceSrc == Workout.CADENCE_SOURCE_QI) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        } else if (cadenceSrc != Workout.CADENCE_SOURCE_NONE) {
            arrayList.add(R.drawable.ic_workout_list_cadence);
        }

        if (heartSrc == Workout.HEARTRATE_SOURCE_BLE || heartSrc == Workout.HEARTRATE_SOURCE_BICI) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        } else if (heartSrc != Workout.HEARTRATE_SOURCE_NONE) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        }

        if (locSrc == Workout.LOCATION_SOURCE_BES_WATCH) {
            arrayList.add(R.drawable.ic_add_bikecomputer);
        } else if (locSrc == Workout.LOCATION_SOURCE_IGPS) {
            arrayList.add(R.drawable.ic_workout_list_igps);
        } else if (locSrc == Workout.LOCATION_SOURCE_BRYTON) {
            arrayList.add(R.drawable.ic_workout_list_bryton);
        } else if (locSrc == Workout.LOCATION_SOURCE_MAGENE) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        } else if (locSrc == Workout.LOCATION_SOURCE_GARMIN) {
            arrayList.add(R.drawable.ic_workout_list_garmin);
        } else if (locSrc == Workout.LOCATION_SOURCE_THINKRIDER) {
            arrayList.add(R.drawable.ic_default);
        } else if (locSrc == Workout.LOCATION_SOURCE_TIC_WATCH || locSrc == Workout.LOCATION_SOURCE_ANDROID_WEAR) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        } else if (locSrc == Workout.LOCATION_SOURCE_SPRINT) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        } else if (locSrc == Workout.LOCATION_SOURCE_XOSS_NAV || locSrc == Workout.LOCATION_SOURCE_XOSS_NAV_PLUS) {
            arrayList.add(R.drawable.ic_default);
        } else if (locSrc == Workout.LOCATION_SOURCE_DISCOVERY) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        } else if (locSrc == Workout.LOCATION_SOURCE_XPLOVA) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        } else if (locSrc == Workout.LOCATION_SOURCE_SUUNTO) {
            arrayList.add(R.drawable.ic_source_suunto);
        } else if (locSrc == Workout.LOCATION_SOURCE_XOSS_G) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        }

        if (isSegment) {
            arrayList.add(R.drawable.ic_workout_list_hr);
        }

        if (is3dWorkout) {
            arrayList.add(R.drawable.ic_workout_list_3d);
        }

        if (slopeGradeMax != null && !slopeGradeMax.isEmpty()) {
            int iconId = 0;
            switch (slopeGradeMax) {
                case SlopeUtils.SLOPE_1_TYPE -> iconId = R.drawable.ic_default;
                case SlopeUtils.SLOPE_2_TYPE -> iconId = R.drawable.ic_default;
                case SlopeUtils.SLOPE_3_TYPE -> iconId = R.drawable.ic_default;
                case SlopeUtils.SLOPE_4_TYPE -> iconId = R.drawable.ic_default;
                case SlopeUtils.SLOPE_HC_TYPE -> iconId = R.drawable.ic_default;
                default -> {
                }
            }
            if (iconId != 0) {
                arrayList.add(iconId);
            }
        }
        if (arrayList.isEmpty()) {
            return null;
        }
        return spliceIcons(context, arrayList, sizeInDip);
    }

    public static Drawable generateLushuFeature(Context context, boolean threedLushu, boolean segment, boolean isCollected, boolean isWorkoutType, boolean isTemp, int caTDF) {
        ArrayList<Drawable> arrayList = new ArrayList<>();
        if (caTDF != 0) {
            Drawable drawable = null;
            if (caTDF == 1) {
                drawable = ContextCompat.getDrawable(context, R.drawable.ic_default);
            } else if (caTDF == 2) {
                drawable = ContextCompat.getDrawable(context, R.drawable.ic_default);
            } else if (caTDF == 3) {
                drawable = ContextCompat.getDrawable(context, R.drawable.ic_default);
            } else if (caTDF == 4) {
                drawable = ContextCompat.getDrawable(context, R.drawable.ic_default);
            } else if (caTDF == 5) {
                drawable = ContextCompat.getDrawable(context, R.drawable.ic_default);
            }
            if (drawable != null) {
                arrayList.add(drawable);
            }
        }
        if (isTemp) {
            arrayList.add(ContextCompat.getDrawable(context, R.drawable.ic_road_book));
        }
        if (isCollected) {
            arrayList.add(ContextCompat.getDrawable(context, R.drawable.ic_lushu_collected));
        }
        if (isWorkoutType) {
            arrayList.add(ContextCompat.getDrawable(context, R.drawable.ic_workout_lushu));
        }
        if (segment) {
            arrayList.add(ContextCompat.getDrawable(context, R.drawable.ic_default));
        }
        if (threedLushu) {
            arrayList.add(ContextCompat.getDrawable(context, R.drawable.ic_threed_lushu));
        }
        return spliceIcons(context, 16, (Drawable[]) arrayList.toArray(new Drawable[arrayList.size()]));
    }
}